package com.att.control;

import java.io.FileInputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.googlecode.aviator.AviatorEvaluator;

public class DeviceEventConfig {
//	private FelEngine fel = new FelEngineImpl();
	
	private String sn;
	
	private String xExpress;
	
	private String yExpress;
	
	private String touchCMD;
	
	private String dragStartCMD;
	
	private String dragProcedureCMD;
	
	private String dragEndCMD;
	
	private String longpressStartCMD;
	
	private String longpressEndCMD;
	
	private String homeCMD;
	
	private String backCMD;
	
	private String menuCMD;
	
	private String searchCMD;
	
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	public String getxExpress() {
		return xExpress;
	}

	public void setxExpress(String xExpress) {
		this.xExpress = xExpress;
	}

	public String getyExpress() {
		return yExpress;
	}

	public void setyExpress(String yExpress) {
		this.yExpress = yExpress;
	}

	public String getTouchCMD() {
		return touchCMD;
	}

	public void setTouchCMD(String touchCMD) {
		this.touchCMD = touchCMD;
	}

	public String getDragStartCMD() {
		return dragStartCMD;
	}

	public void setDragStartCMD(String dragStartCMD) {
		this.dragStartCMD = dragStartCMD;
	}

	public String getDragProcedureCMD() {
		return dragProcedureCMD;
	}

	public void setDragProcedureCMD(String dragProcedureCMD) {
		this.dragProcedureCMD = dragProcedureCMD;
	}

	public String getDragEndCMD() {
		return dragEndCMD;
	}

	public void setDragEndCMD(String dragEndCMD) {
		this.dragEndCMD = dragEndCMD;
	}

	public String getLongpressStartCMD() {
		return longpressStartCMD;
	}

	public void setLongpressStartCMD(String longpressStartCMD) {
		this.longpressStartCMD = longpressStartCMD;
	}

	public String getLongpressEndCMD() {
		return longpressEndCMD;
	}

	public void setLongpressEndCMD(String longpressEndCMD) {
		this.longpressEndCMD = longpressEndCMD;
	}

	public String getHomeCMD() {
		return homeCMD;
	}

	public void setHomeCMD(String homeCMD) {
		this.homeCMD = homeCMD;
	}

	public String getBackCMD() {
		return backCMD;
	}

	public void setBackCMD(String backCMD) {
		this.backCMD = backCMD;
	}

	public String getMenuCMD() {
		return menuCMD;
	}

	public void setMenuCMD(String menuCMD) {
		this.menuCMD = menuCMD;
	}

	public String getSearchCMD() {
		return searchCMD;
	}

	public void setSearchCMD(String searchCMD) {
		this.searchCMD = searchCMD;
	}
	
	public DeviceEventConfig parseConfig(FileInputStream in) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document dom = builder.parse(in);
		Element root = dom.getDocumentElement();
		sn = root.getAttribute("sn");
		xExpress = ((Element)root.getElementsByTagName("x").item(0)).getTextContent();
		yExpress = ((Element)root.getElementsByTagName("y").item(0)).getTextContent();
		touchCMD = ((Element)root.getElementsByTagName("touch").item(0)).getTextContent();
		dragStartCMD = ((Element)root.getElementsByTagName("drag_start").item(0)).getTextContent();
		dragProcedureCMD = ((Element)root.getElementsByTagName("drag_procedure").item(0)).getTextContent();
		dragEndCMD = ((Element)root.getElementsByTagName("drag_end").item(0)).getTextContent();
		longpressStartCMD = ((Element)root.getElementsByTagName("longpress_start").item(0)).getTextContent();
		longpressEndCMD = ((Element)root.getElementsByTagName("longpress_end").item(0)).getTextContent();
		homeCMD = ((Element)root.getElementsByTagName("home").item(0)).getTextContent();
		backCMD = ((Element)root.getElementsByTagName("back").item(0)).getTextContent();
		menuCMD = ((Element)root.getElementsByTagName("menu").item(0)).getTextContent();
		searchCMD = ((Element)root.getElementsByTagName("search").item(0)).getTextContent();
		return this;
	}
	
	public String caculateX(int x) {
		return (String)( AviatorEvaluator.execute(xExpress.replaceFirst("x", String.valueOf(x))));
	}
	
	public String caculateY(int y) {
		return (String)( AviatorEvaluator.execute(yExpress.replaceFirst("y", String.valueOf(y))));
	}
	
	public String getTouchCMD(String x, String y) {
		return touchCMD.replaceFirst("x", x).replaceFirst("y", y);
	}
	
	public String getDragStartCMD(String startx, String starty) {
		return dragStartCMD.replaceFirst("startx", startx).replaceFirst("starty", starty);
	}
	
	public String getDragProcedureCMD(String x, String y) {
		return dragProcedureCMD.replaceFirst("x", x).replaceFirst("y", y);
	}
	
	public String getDragEndCMD(String endx, String endy) {
		return dragEndCMD.replaceFirst("endx", endx).replaceFirst("endy", endy);
	}
	
	public String getLongpressStart(String startx, String starty) {
		return longpressStartCMD.replaceFirst("startx", startx).replaceFirst("starty", starty);
	}
	
	public String getLongpressEnd(String endx, String endy) {
		return longpressStartCMD.replaceFirst("endx", endx).replaceFirst("endy", endy);
	}
	
}
